package com.shiro.service.impl;

import com.shiro.mapper.UserRoleMapper;
import com.shiro.pojo.User;
import com.shiro.pojo.UserRole;
import com.shiro.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public void setRoles(User user, long[] roleIds) {
        userRoleMapper.deleteByUser(user.getId());
        UserRole userRole = new UserRole();
        userRole.setUid(user.getId());
        for (long roleId : roleIds) {
            userRole.setRid(roleId);
            userRoleMapper.insertSelective(userRole);
        }
    }

    @Override
    public void deleteByUser(long userId) {
        userRoleMapper.deleteByUser(userId);
    }

    @Override
    public void deleteByRole(long roleId) {
        userRoleMapper.deleteByRole(roleId);
    }
}
